import os, sys
import pygame
from pygame.locals import *
import copy

if not pygame.font: print('Warning, fonts disabled')
if not pygame.mixer: print('Warning, sound disabled')

class Level:
    # 
    map ={
        1: [
            [2, 1, 1, 1, 1, 1, 1, 2],
            [2, 1, 5, 0, 0, 0, 1, 2],
            [2, 1, 0, 0, 4, 0, 1, 2],
            [2, 1, 1, 3, 1, 1, 1, 2],
            [2, 2, 1, 1, 1, 2, 2, 2],
            [2, 2, 2, 2, 2, 2, 2, 2],
            [2, 2, 2, 2, 2, 2, 2, 2],
            [2, 2, 2, 2, 2, 2, 2, 2]
        ]
    }

    #关卡图 0 1 2 3 4 5 6
    blank = pygame.image.load("res/GroundGravel_Sand.png")
    block = pygame.image.load("res/Wall_Brown.png")
    grass = pygame.image.load("res/GroundGravel_Grass.png")
    box_right = pygame.image.load("res/EndPoint_Red.png")
    box = pygame.image.load("res/Crate_Red.png")
    person = pygame.image.load("res/Character4.png")

    def __init__(self, level, window):
        self.window = window
        self.level = level
        self.map_data = copy.deepcopy(Level.map[self.level])
        #创建队列，记录撤销
        # self.que = back_queue()

    @classmethod
    def p_dict(cls):
        dic = {
            0:cls.blank,
            1:cls.block,
            2:cls.grass,
            3:cls.box_right,
            4:cls.box,
            5:cls.person
        }
        return dic

    #关卡贴图
    def display(self):
        size = 64
        dic = Level.p_dict()
        
        #记录下正确的点的列表
        li = list()
        for y in range(len(self.map_data)):
            for x in range(len(self.map_data[y])):
                img = dic[self.map_data[y][x]]
                img_size = img.get_rect()
                px = size*(x+1) - img_size[2]/2
                py = size*(y+1) - img_size[3]/2
                self.window.blit(dic[0],(px, py))
                self.window.blit(img, (px, py))
        return li


pygame.init()
width = 800
height = 600
screen = pygame.display.set_mode((800,600),0,32)
pygame.display.set_caption("精灵类测试")
font = pygame.font.Font(None, 18)
framerate = pygame.time.Clock()
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
screen.blit(background, (0, 0))

#设立关卡
level = Level(1, screen)
#游戏关卡贴图 #记录正确点的位置
li = level.display()

player = pygame.image.load('res/Character1.png').convert_alpha()
playImageSize = player.get_rect()
playImageSize.center = width / 2, height + 50
screen.blit(player, (0,0) )
pygame.display.flip()

while 1:
    for event in pygame.event.get():
        if event.type in (pygame.QUIT, pygame.KEYDOWN):
            sys.exit()